﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Commons.Utils;
using Shpm.System;
using Shpm.App.Commons.Db.Beans.Master;

public partial class App_Input_MessageSrhForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            SysUserManager manager = new SysUserManager(Context);
            SysUserBean sysUserBean = manager.getSysUserBean();
            MstSysUserBean mstSysUserBean =
                sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
            MstEmployeeBean mstEmpBean =
                sysUserBean == null ? null : sysUserBean.getEmployeeBean();
            string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;
            string emplyId = mstSysUserBean == null ? "" : mstSysUserBean.EmployeeId;
            ViewState["EMPLOYEE_ID"] = emplyId;

            MessageBean dataBean = new MessageBean();
            MessagesDao Dao = new MessagesDao();
            dataBean.EmployeeIdChar = emplyId;
            if (Context.Items.Contains("BEGIN_DATE") && !string.IsNullOrEmpty(Convert.ToString(Context.Items["BEGIN_DATE"])))
                dataBean.BeginDate = Convert.ToDateTime(Context.Items["BEGIN_DATE"]);
            if (Context.Items.Contains("END_DATE") && !string.IsNullOrEmpty(Convert.ToString(Context.Items["END_DATE"])))
                dataBean.EndDate = Convert.ToDateTime(Context.Items["END_DATE"]);

            this.txtBeginDate.Text = Convert.ToString(Context.Items["BEGIN_DATE_TXT"]);
            this.txtEndDate.Text = Convert.ToString(Context.Items["END_DATE_TXT"]);
            IList dataList = Dao.FindMessageSrh(dataBean);

            GridView1.DataSource = dataList;
            GridView1.DataBind();
        }
        
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == -1) return;
        MessageBean dataBean = (MessageBean)e.Row.DataItem;
        if(string.IsNullOrEmpty(dataBean.Subject))
        {
            e.Row.Cells[0].Text = dataBean.Message.Length>10?dataBean.Message.Substring(0,10)+"...":dataBean.Message;
        }
        else
        {
             e.Row.Cells[0].Text =  dataBean.Subject;
        }
       
        e.Row.Cells[1].Text = string.Format("{0}({1})", dataBean.EmployeeName, dataBean.EmployeeId);
        if (Convert.ToInt32(dataBean.Cnt) > 0)
        {
            e.Row.Cells[4].Text = "阅读";
        }
        else
        {
            e.Row.Cells[4].Text = "--";
        }

        HyperLink link = ((HyperLink)e.Row.FindControl("search"));
        link.Attributes.Add("onclick", "popupRead('" + dataBean.MessageId.ToString() + "')");
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int row = (sender as GridView).SelectedIndex;
        Context.Items["MESSAGE_ID"] = (sender as GridView).DataKeys[row].Value.ToString();
        Context.Items["BEGIN_DATE"] = ViewState["BEGIN_DATE"];
        Context.Items["END_DATE"] = ViewState["END_DATE"];
        Context.Items["BEGIN_DATE_TXT"] = this.txtBeginDate.Text;
        Context.Items["END_DATE_TXT"] = this.txtEndDate.Text;

        ControllerUtils.Transfer(Context, "~/App/Input/MessageReadForm.aspx", true);    
    }
    protected void btnSrh_Click(object sender, EventArgs e)
    {
        if(this.ComValidator1.DoValidator() == false )
        {
            ((Design_MasterPage)this.Master).InfoMessage = ComValidator1.ErrorMessage;
            return;
        }
        MessageBean dataBean = new MessageBean();
        MessagesDao Dao = new MessagesDao();
        dataBean.EmployeeIdChar = Convert.ToString(ViewState["EMPLOYEE_ID"]);
        if (!"".Equals(this.txtBeginDate.Text))
            dataBean.BeginDate = DateUtils.convertToDate(this.txtBeginDate.Text);
        if (!"".Equals(this.txtEndDate.Text))
            dataBean.EndDate = DateUtils.convertToDate(this.txtEndDate.Text);

        ViewState["BEGIN_DATE"] = this.txtBeginDate.Text;
        ViewState["END_DATE"] = this.txtEndDate.Text;
        IList dataList = Dao.FindMessageSrh(dataBean);

        GridView1.DataSource = dataList;
        GridView1.DataBind();
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Context.Items["STATUS"] = "NEW";
        Context.Items["BEGIN_DATE"] = ViewState["BEGIN_DATE"];
        Context.Items["END_DATE"] = ViewState["END_DATE"];
        Context.Items["BEGIN_DATE_TXT"] = this.txtBeginDate.Text;
        Context.Items["END_DATE_TXT"] = this.txtEndDate.Text;

        ControllerUtils.Transfer(Context, "~/App/Input/MessageUpdForm.aspx", true);    
    }
}
